package com.download.backstage.controller;

import com.download.backstage.entity.User;
import com.download.web.entity.SysUser;
import com.download.web.entity.model.Page;
import com.download.web.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 用户管理
 * @author: cmy
 * @create: 2019-02-24 15:06
 **/
@RestController
@RequestMapping("/backstage/user")
public class BackUserController {

    @Autowired
    private ISysUserService userService;

    @RequestMapping("/userPage")
    public Page<User> userPage(Integer pageIndex, Integer pageSize){
        return userService.userPage(pageIndex,pageSize);
    }

    @RequestMapping("/userAdd")
    public void userAdd(@RequestBody SysUser user){
         userService.save(user);
    }
    @RequestMapping("/findUserById")
    public SysUser findUserById(Integer id){
        return userService.getById(id);
    }
    @RequestMapping("/userUpdate")
    public void userUpdate(@RequestBody SysUser user){
        userService.updateById(user);
    }

    @Transactional
    @RequestMapping("/userDel")
    public void userDel(Integer id){
        userService.removeById(id);
    }

    @RequestMapping("/login")
    public String login(String account, String pwd){
        try {
            String msg="";
            if(account!=null&&!account.equals("")&&pwd!=null||!pwd.equals("")){
                if(account.equals("admin")&&pwd.equals("admin")){
                    msg="0";
                }else{
                    msg="帐号密码错误";
                }
            }else{
                msg="请填写正确帐号密码";
            }
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            return "系统异常";
        }
    }
}
